package com.hr.system.controller;

import java.util.List;

import com.hr.common.core.domain.entity.SysUser;
import com.hr.common.utils.ShiroUtils;
import com.hr.system.ApprovalStatusEnum;
import com.hr.system.domain.SysLeave;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hr.common.annotation.Log;
import com.hr.common.enums.BusinessType;
import com.hr.system.domain.SysOverWork;
import com.hr.system.service.ISysOverWorkService;
import com.hr.common.core.controller.BaseController;
import com.hr.common.core.domain.AjaxResult;
import com.hr.common.utils.poi.ExcelUtil;
import com.hr.common.core.page.TableDataInfo;

/**
 * 加班Controller
 * 
 * @author ruoyi
 * @date 2021-03-24
 */
@Controller
@RequestMapping("/system/work")
public class SysOverWorkController extends BaseController
{
    private String prefix = "system/work";

    @Autowired
    private ISysOverWorkService sysOverWorkService;

    @RequiresPermissions("system:work:view")
    @GetMapping()
    public String work()
    {
        return prefix + "/work";
    }


    @RequiresPermissions("system:work:toNeedWork")
    @GetMapping("/toNeedWork")
    public String toNeedLeave(){
        return prefix + "/needWork";
    }

    @RequiresPermissions("system:work:toApproval")
    @GetMapping("/toWorkApproval")
    public String toApproval(){
        return prefix + "/approval";
    }

    @PostMapping("/myDeptWorkList")
    @ResponseBody
    public TableDataInfo myDeptLeaveList(SysOverWork sysOverWork){
        SysUser sysUser = ShiroUtils.getSysUser();
        sysOverWork.setDeptId(sysUser.getDeptId().intValue());
        sysOverWork.setWorkStatus(ApprovalStatusEnum.WAIT_LEADER_APPROVAL.getStatus());
        sysOverWork.setTenantId(Integer.toUnsignedLong(sysUser.getTenantId()));
        startPage();
        List<SysOverWork> list = sysOverWorkService.selectSysOverWorkList(sysOverWork);
        return getDataTable(list);
    }

    /**
     * 提交请假审批
     */
    @ResponseBody
    @PostMapping("/approval")
    public AjaxResult approval(Long id) {
        sysOverWorkService.approval(id);
        return AjaxResult.success("提交审核成功");
    }

    /**
     * 查询加班列表
     */
    @RequiresPermissions("system:work:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysOverWork sysOverWork)
    {
        startPage();
        List<SysOverWork> list = sysOverWorkService.selectSysOverWorkList(sysOverWork);
        return getDataTable(list);
    }

    @PostMapping("/myWorkList")
    @ResponseBody
    public TableDataInfo iWantWork(SysOverWork sysOverWork){

        SysUser sysUser = ShiroUtils.getSysUser();
        sysOverWork.setUserId(sysUser.getUserId());
        sysOverWork.setTenantId(Integer.toUnsignedLong(sysUser.getTenantId()));
        startPage();
        List<SysOverWork> list = sysOverWorkService.selectSysOverWorkList(sysOverWork);
        return getDataTable(list);
    }

    /**
     * 领导通过请假审批
     */
    @ResponseBody
    @PostMapping("/leaderApproval")
    public AjaxResult leaderApproval(Long id) {
        sysOverWorkService.leaderApproval(id);
        return AjaxResult.success("审核成功");
    }

    /**
     * 导出加班列表
     */
    @RequiresPermissions("system:work:export")
    @Log(title = "加班", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysOverWork sysOverWork)
    {
        List<SysOverWork> list = sysOverWorkService.selectSysOverWorkList(sysOverWork);
        ExcelUtil<SysOverWork> util = new ExcelUtil<SysOverWork>(SysOverWork.class);
        return util.exportExcel(list, "work");
    }

    /**
     * 新增加班
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存加班
     */
    @RequiresPermissions("system:work:add")
    @Log(title = "加班", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysOverWork sysOverWork)
    {
        int tenantId = ShiroUtils.getSysUser().getTenantId();
        Long userId = ShiroUtils.getSysUser().getUserId();
        sysOverWork.setTenantId(Integer.toUnsignedLong(tenantId));
        sysOverWork.setUserId(userId);
        return toAjax(sysOverWorkService.insertSysOverWork(sysOverWork));
    }

    /**
     * 修改加班
     */
    @GetMapping("/edit/{overWorkId}")
    public String edit(@PathVariable("overWorkId") Long overWorkId, ModelMap mmap)
    {
        SysOverWork sysOverWork = sysOverWorkService.selectSysOverWorkById(overWorkId);
        mmap.put("sysOverWork", sysOverWork);
        return prefix + "/edit";
    }

    /**
     * 修改保存加班
     */
    @RequiresPermissions("system:work:edit")
    @Log(title = "加班", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysOverWork sysOverWork)
    {
        return toAjax(sysOverWorkService.updateSysOverWork(sysOverWork));
    }

    /**
     * 删除加班
     */
    @RequiresPermissions("system:work:remove")
    @Log(title = "加班", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysOverWorkService.deleteSysOverWorkByIds(ids));
    }

}
